#ATM存取款模拟

#用户信息
listUser = [{"name":"a", "password":"1", "money":100},
            {"name":"b", "password":"2", "money":200},
            {"name":"c", "password":"3", "money":300}]

bLoginSuc = False		#用户是否登录成功
strCurUser = str()		#当前登录用户

#登录
def Login(name, password):
	global bLoginSuc
	global strCurUser

	bExist = False		#用户是否存在

	#遍历用户信息，比对用户名及密码是否正确
	for i in range(len(listUser)):
		if name == listUser[i]["name"]:
			bExist = True

			if password == listUser[i]["password"] :
				print("用户登录成功，用户名:{}\n".format(name))
				bLoginSuc = True
				strCurUser = listUser[i]["name"]
				break
			else:
				print("密码输入错误，请重新输入，用户名:{}\n".format(name))

	if False == bExist:
		print("用户不存在，请重新输入，用户名:{}\n".format(name))


#退出
def Exit():
	global bLoginSuc

	if False == bLoginSuc:
		print("用户未登录，退出失败\n")
		return False
	else:
		print("用户退出成功\n")
		bLoginSuc = False
		return True

#查询
def InquireMoney():
	global strCurUser

	#遍历用户信息，找到当前登录用户，打印存款数额
	for i in range(len(listUser)):
		if strCurUser == listUser[i]["name"]:
			print("用户存款额为{}\n".format(listUser[i]["money"]))

#存钱
def SaveMoney(money):
	global strCurUser

	if money < 0:
		print("存款数额输入错误，{}\n".format(money))
		return

	#遍历用户信息，找到当前登录用户，在原有存款数额上进行增加并打印
	for i in range(len(listUser)):
		if strCurUser == listUser[i]["name"]:
			oldMoney = listUser[i]["money"]
			listUser[i]["money"] = oldMoney + money
			print("用户存款额为{}\n".format(listUser[i]["money"]))

#取钱
def DrawMoney(money):
	global bLoginSuc
	global strCurUser

	if money < 0:
		print("存款数额输入错误，{}\n".format(money))
		return

	#遍历用户信息，找到当前登录用户，在原有存款数额上进行减少并打印
	for i in range(len(listUser)):
		if strCurUser == listUser[i]["name"]:
			oldMoney = listUser[i]["money"]

			if money > oldMoney:
				print("取款数额大于存款数额，取钱失败\n")
				return
			else:
				listUser[i]["money"] = oldMoney - money
				print("用户余额为{}\n".format(listUser[i]["money"]))

############################################################################

while True:
	strSelect = input("ATM存取款模拟，请选择操作方式\n1.登录 2.退出 3.查询 4.存钱 5.取钱\n")

	if "1" == strSelect:						#登录
		print("=== 请输入用户名和密码 === ")
		name = input("用户名:")
		password = input("密码:")

		Login(name, password)
	elif "2" == strSelect:						#退出
		Exit()
	elif "3" == strSelect:						#查询
		if False == bLoginSuc:
			print("用户未登录，查询失败\n")
			continue

		InquireMoney()
	elif "4" == strSelect:						#存钱
		if False == bLoginSuc:
			print("用户未登录，存钱失败\n")
			continue

		print("=== 请输入存款数额 === ")
		money = input("存款数额:")

		SaveMoney(int(money))
	elif "5" == strSelect:						#取钱
		if False == bLoginSuc:
			print("用户未登录，取钱失败\n")
			continue

		print("=== 请输入取款数额 === ")
		money = input("取款数额:")

		DrawMoney(int(money))
	else:
		print("输入错误，请重新输入")
